<template>
<el-dialog title="添加联系方式" :visible.sync="visible" append-to-body width="400px">
    <el-form :model="ruleForm" label-width="80px" label-position="left">
        <el-form-item label="角色">
            <el-select v-model="ruleForm.role" placeholder="请选择" class="w100">
                <el-option v-for="(item, key) in roleTypes" :key="key" :label="item.title" :value="item.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="类型">
            <el-select v-model="ruleForm.contact_type" placeholder="请选择" class="w100">
                <el-option v-for="(item, key) in contactTypes" :key="key" :label="item.title" :value="item.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item :label="contactLabel">
            <el-input v-model="ruleForm.contact" autocomplete="off"></el-input>
            <el-alert v-if="ruleForm.contact_type == 3" title="例: 021-1234567,0755-12345678-123" type="info" :closable="false" style="margin-top: 5px">
            </el-alert>
        </el-form-item>
        <el-form-item label="默认">
            <el-select v-model="ruleForm.is_default" placeholder="请选择" class="w100">
                <el-option label="否" :value="0"></el-option>
                <el-option label="是" :value="1"></el-option>
            </el-select>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="appendContact">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    appendContactUniqueApi
} from "@/api/customer/contact";

import {
    contactTypes,
    roleTypes
} from "@/config/reflection.js";

const ruleForm = {
    member_id: "",
    contact_type: "1",
    role: "1",
    contact: "",
    is_default: 0,
};

export default {
    name: "AppendContact",
    inject: ["customerDetail"],
    data() {
        return {
            visible: false,
            contactTypes: contactTypes,
            roleTypes: roleTypes,
            ruleForm: Object.assign({}, ruleForm),
        };
    },
    computed: {
        contactLabel() {
            const contactType = this.contactTypes.find(
                (item) => item.value === this.ruleForm.contact_type
            );
            return contactType ? contactType.title : "";
        },
    },
    methods: {
        showDialog(member_id) {
            this.ruleForm = Object.assign({}, ruleForm);
            this.ruleForm.member_id = member_id;
            this.visible = true;
        },
        closeDialog() {
            this.visible = false;
        },
        // 添加联系方式
        appendContact() {
            let contactType = contactTypes.find(
                (ele) => ele.value == this.ruleForm.contact_type
            );
            if (!contactType.pattern.test(this.ruleForm.contact)) {
                this.$message.error(contactType.err);
                return false;
            }
            // 系统联系方式唯一性检查
            appendContactUniqueApi(this.ruleForm).then((res) => {
                if (res.code == "200") {
                    this.customerDetail.getAllocateDetail();
                    this.closeDialog();
                } else {
                    this.$message({
                        type: "error",
                        message: res.msg,
                    });
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.w100 {
    width: 100%;
}
</style>
